import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'
// import Welcome from '../components/Welcome.vue'
const Login = () => import(/* webpackChunkName: 'login_home_welcome' */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName: 'login_home_welcome' */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunkName: 'login_home_welcome' */ '../components/Welcome.vue')

// import Users from '../components/user/Users.vue'
// import Rights from '../components/power/Rights.vue'
// import Roles from '../components/power/Roles.vue'
const Users = () => import(/* webpackChunkName: 'Users_Rights_Roles' */ '../components/user/Users.vue')
const Rights = () => import(/* webpackChunkName: 'Users_Rights_Roles' */ '../components/power/Rights.vue')
const Roles = () => import(/* webpackChunkName: 'Users_Rights_Roles' */ '../components/power/Roles.vue')

// import Cate from '../components/goods/Cate.vue'
// import Params from '../components/goods/Params.vue'
const Cate = () => import(/* webpackChunkName: 'Cate_Params' */ '../components/goods/Cate.vue')
const Params = () => import(/* webpackChunkName: 'Cate_Params' */ '../components/goods/Params.vue')

// import GoodsList from '../components/goods/List.vue'
// import Add from '../components/goods/Add.vue'
const GoodsList = () => import(/* webpackChunkName: 'GoodsList_Add' */ '../components/goods/List.vue')
const Add = () => import(/* webpackChunkName: 'GoodsList_Add' */ '../components/goods/Add.vue')

// import Order from '../components/order/Order.vue'
// import Report from '../components/report/Report.vue'
const Order = () => import(/* webpackChunkName: 'Order_Report' */ '../components/order/Order.vue')
const Report = () => import(/* webpackChunkName: 'Order_Report' */ '../components/report/Report.vue')

Vue.use(VueRouter)

// 路由规则
const routes = [
  // 访问根目录 重定向到登录页面
  { path: '/', redirect: '/login' },
  // 1 登录页面
  { path: '/login', component: Login },
  // 2 首页
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      // 3 欢迎页面
      { path: '/welcome', component: Welcome },
      // 4 用户列表页面
      { path: '/users', component: Users },
      // 5 权限页面
      { path: '/rights', component: Rights },
      // 6 角色页面
      { path: '/roles', component: Roles },
      // 7 商品分类页面
      { path: '/categories', component: Cate },
      // 8 分类参数页面
      { path: '/params', component: Params },
      // 9 商品列表页面
      { path: '/goods', component: GoodsList },
      // 10 添加商品页面
      { path: '/goods/add', component: Add },
      // 11 订单列表页面
      { path: '/orders', component: Order },
      // 12 数据报表页面
      { path: '/reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  // next：函数 放行
  // next()放行 next('/login')强制跳转

  if (to.path === '/login') {
    return next()
  }
  // 获取 token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    return next('/login')
  }
  next()
})

export default router
